package net.myspring.middle.web;

import net.myspring.common.domain.SearchEntity;
import net.myspring.common.utils.Const;
import net.myspring.common.utils.Message;
import net.myspring.common.utils.RequestUtils;
import net.myspring.middle.domain.Department;
import net.myspring.middle.service.DepartmentService;
import net.myspring.middle.validator.DepartmentValidator;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

/**
 * Created by admin on 2016/8/12.
 */
@Controller
@RequestMapping(value = "sys/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private DepartmentValidator departmentValidator;

    @RequestMapping(value = "list")
    @PreAuthorize("hasAnyAuthority('PERM_sys:department:list:view')")
    public String list(Model model, HttpServletRequest request) {
        SearchEntity searchEntity = RequestUtils.getSearchEntity(request, Department.class.getName());
        model.addAttribute("page", departmentService.findPage(searchEntity.getPageable(), searchEntity.getParams()));
        model.addAttribute("s", searchEntity);
        return "sys/departmentList";
    }

    @RequestMapping(value = "form")
    @PreAuthorize("hasAnyAuthority('PERM_sys:department:list:edit')")
    public String form(Model model, Department department) {
        model.addAttribute("department", department.getId() == null ? department : departmentService.findOne(department.getId()));
        addAttribute(model);
        return "sys/departmentForm";
    }

    @RequestMapping(value = "save")
    @PreAuthorize("hasAnyAuthority('PERM_sys:department:list:edit')")
    public String save(Model model, Department department, BindingResult bindingResult, RedirectAttributes redirectAttributes) {
        boolean isCreate = department.isCreate();
        departmentValidator.validate(department, bindingResult);
        if (bindingResult.hasErrors()) {
            model.addAttribute("bindingResult", bindingResult);
            addAttribute(model);
            return "sys/departmentForm";
        }
        departmentService.save(department);
        redirectAttributes.addFlashAttribute("message", new Message("菜单保存成功"));
        if (isCreate) {
            return "redirect:/sys/department/form";
        } else {
            return "redirect:/sys/department/list";
        }
    }

    @RequestMapping(value = "delete")
    @PreAuthorize("hasAnyAuthority('PERM_sys:department:list:delete')")
    public String delete(Model model, Department department, RedirectAttributes redirectAttributes) {
        departmentService.delete(department);
        redirectAttributes.addFlashAttribute("message", new Message("部门删除成功"));
        return "redirect:/sys/department/list";
    }

    @RequestMapping(value = "checkName")
    @ResponseBody
    public boolean checkName(Department department) {
        return departmentService.checkName(department);
    }


    private void addAttribute(Model model) {
        model.addAttribute("departmentList", departmentService.findAll());
        model.addAttribute("bools", Const.BoolEnum.getMap());
    }
}
